<template>
  <div>
    <div class="container-body">
      <div class="main-body-swap">
        <div class="main-body-header-nav">
          <div class="main-body-header-item" v-for="workClassification in workClassificationList">
              <div class="title" v-html="workClassification.topicinfo"></div>
              <div class="body">
                <div :class="[workClassificationchild.selected === true ? ' swap-span active':'swap-span']" v-for="workClassificationchild in workClassification.topicClassificationList">
                  <span v-html="workClassificationchild.topicinfo" @click="lookFilterWorkInfo(workClassificationchild)">
                  </span>
                </div>
              </div>
          </div>
        </div>

        <div class="main-body" ref="mainBodyDiv">
          <div class="card-item-body" v-for="(workInfo,index) in workInfoList" @click="lookWorkInfo(workInfo.uuid)" :key="index">
            <div class="card-content">
              <div class="card-item">
                <span class="card-title-font" >{{index+1}}. {{workInfo.title}}</span>
              </div>
              <span class="card-content-body" v-html="workInfo.content"></span>
            </div>
            <div class="card-footer">
              <span>关注({{workInfo.followNum}})</span>
              <span>回答({{workInfo.commentNum}})</span>
            </div>
          </div>

          <div class="load-more" v-show="!queryFlag">
            <span>没有更多了</span>
          </div>
        </div>
      </div>
      <div class="rightInfo">
        <div class="global-writer">
          <div class="global-writer-info">
            <a class="title" @click="writeArticle()">写文章</a>
            <a class="title" @click="writeProblem()">写问题</a>
            <a class="title">写回答</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>

  export default {
    components: {
    },
    data(){
      return {
        workClassificationList:[],
        workInfoList:[],
        topClassIficationList:[],
        topicClassificationUuids:[],
        searchContent:"",
        pageSize: 8,
        pageNum: 0,
        isClearData:false,
        queryFlag: true//如果返回数据为空的话,就把这个参数设置为false,避免重复请求数据
      }
    },
    name: "searchResultInfo",
    created:function(){    //creadted 实例创建完成之后立即调用,此时实例已经完成了:数据观测,属性和方法的运算.watch和event事件的回调,挂载阶段没有开始;因此,该函数一般是用来初始化数据的 ,html渲染完成之后才会调用mounted这个函数,dom操作一般在mounted函数中执行

      this.searchContent = this.$route.query.searchContent;
      if(this.queryFlag === true){
        this.queryWorkInfo();
      }
      this.initClassification();
    },
    methods:{
      searchWorkInfo:function (searchContent) {//搜索任务详情
        this.searchContent = searchContent;
        this.queryWorkInfo();
      },
      handleScroll:function () {

        const _scrollTop = document.documentElement.scrollTop;//可滚动容器超出当前窗口显示范围的高度
        const clientHeight = document.documentElement.clientHeight;//屏幕的尺寸高度
        const contentHeight = document.body.offsetHeight-5;//可滚动屏幕大小

        if (_scrollTop + clientHeight >= contentHeight){//加载更多
          this.isClearData = false;
          this.queryWorkInfo();
        }
      },
      queryWorkInfo:function () {
        if (this.queryFlag === false && !this.isClearData){
          return
        }

        this.pageNum = this.pageNum + 1;
        this.$axios({
          method: 'post',
          url: '/solvePlat/workInfo/selectWorkInfo',
          data: {
            pageSize: this.pageSize,
            pageNum: this.pageNum,
            content: this.searchContent,
            topicClassificationUuids:this.topicClassificationUuids.join(",")
          }
        })
          .then(res => {
            if (res.data.success === true){//成功
              if (res.data.data.list.length != 0 || this.isClearData){
                if (this.isClearData){
                  this.workInfoList = [];
                  this.queryFlag = true;
                }
                this.workInfoList.push.apply(this.workInfoList,res.data.data.list)
              }
              else{
                this.queryFlag = false;
              }
            }
          })
          .catch(err => {
            console.log('err', err)
          })
      },
      writeArticle:function () {
        let writeArticle = this.$router.resolve({
          name: 'writeArticle'
        })
        window.open(writeArticle.href, '_blank');
      },
      writeProblem:function () {
        let writeProblem = this.$router.resolve({
          name: 'writeProblem'
        })
        window.open(writeProblem.href, '_blank');
      },
      lookWorkInfo:function (uuid) {//查看当前任务详情信息
        this.addWorkTaskLookNum(uuid);
        this.$router.push({
          name:"ProblemInfo",
          query:{
            uuid:uuid
          }
        })

      },
      addWorkTaskLookNum:function (workInfoId) {

        this.$axios({
          method: 'get',
          url: '/solvePlat/workInfo/addBrowseNum?uuid='+workInfoId,
        })
          .then(res => {
            if (res.data.success === true){//成功
              if (res.data.data.list.length != 0){
              }
              else{
              }
            }
          })
          .catch(err => {
            console.log('err', err)
          })

      },
      initClassification:function () {
        this.$axios({
          method:'get',
          url:"/solvePlat/topicClassification/selectTopicClassificationListByTree"
        }).then(res=>{
          if (res.data.success === true){
            this.workClassificationList = res.data.data;
          } else{

          }
        })
      },
      lookFilterWorkInfo:function (workClassificationchild) {
          debugger
          if (!workClassificationchild.selected){
            this.topicClassificationUuids.push(workClassificationchild.uuid);
          } else{
            var index = this.topicClassificationUuids.indexOf(workClassificationchild.uuid);
            if (index > -1) {
              this.topicClassificationUuids.splice(index, 1);
            }
          }
          workClassificationchild.selected = !workClassificationchild.selected ;
          this.resetLookInitData();
          this.queryWorkInfo();
      },
      resetLookInitData:function(){
        this.isClearData = true;
        this.queryFlag = true;
        this.pageSize = 8;
        this.pageNum = 0;
      }
    },
    mounted:function () {
      window.addEventListener('scroll', this.handleScroll)
    }
  }
</script>



<style scoped>
  .active{
    background-color: #0084ff!important;
    color: white!important;
  }
  .main-body-header-item .title{
    display: inline-block;
  }

  .body .swap-span{
    background-color: #e5f2ff;
    display: inline-block;
    height: 31px;
    line-height: 31px;
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 10px;
    color: #0084ff;
    margin-left: 10px;
    cursor: pointer;
  }

  .body{
    display: inline-block;
    margin-left: 10px;
  }
  .load-more{
    text-align: center;
    width: 100%;
    font-size: 15px;
    font-weight: 600;
  }

  .rightInfo{
    display: inline-block;
    background-color: #f6f6f6;
    margin-left: 15px;
    width: 15%;
    position: sticky;
    top: 74px;
    vertical-align: top;
  }

  .global-writer-info{
    display: flex;
    justify-content: space-around;
  }

  .global-writer{
    background-color: white;
    height: 50px;
    padding-top: 40px;
  }








  .global-writer-info .title{
    cursor: pointer;
    font-weight: 100;
    font-size: 18px;
  }

  .main-body{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding-right: 10px;
    padding-bottom: -10px;
  }

  .main-body-swap{
    display: inline-block;
    width: 60%;
    background-color: white;
    padding: 10px 10px;
    margin-left: 10%;
    margin-top: 10px;
  }


  .card-item-body{
    display: inline-block;
    padding: 10px 10px;
    border: solid 1px #eeeeee;
    margin-left: 10px;
    margin-bottom: 10px;
    width: 100%;
    height:  96px;
  }
  .card-item-body:hover{
    box-shadow: 0px 0px 8px #cccccc;
    cursor: pointer;
  }
  .card-title-font{
    font-size: 18px;
    font-weight: 600;
    color: #1a1a1a;
    vertical-align: middle;
  }
  .card-content-body{
    display: block;
    height: 54px;
    margin-top: 10px;
    width: 80%;
  }

  .main-body-header-nav{
    margin-left: 12px;
    margin-top: 15px;
    margin-bottom: 14px;
    font-size: 15px;
    font-weight: 600;
    border-bottom: 1px solid #dccce5a6;
    padding-bottom: 10px;
  }

  .active{
    color: #0084ff;
  }

  .main-body-header-nav .span-margin{
    margin-right: 10px;
  }
  .card-item-status-unfindish{
    background-color: #f90;
    color: white;
    padding: 6px 8px;
    vertical-align: middle;
  }
  .card-item{
    display: inline-block;
    vertical-align: text-bottom;
  }
  .card-item-status{
    display: inline-block;
    vertical-align: text-bottom;
    padding-left: 20px;
  }

  .container-body{
    background-color: #f6f6f6;
    min-height: 790px;
  }
</style>
